package com.dongdong.service.impl;

import com.dongdong.mapper.TbSellerMapper;
import com.dongdong.model.TbSeller;
import com.dongdong.model.TbSellerExample;
import com.dongdong.page.PageParams;
import com.dongdong.service.SellerService;
import com.dongdong.util.ShiroUtils;
import com.dongdong.vo.SellerVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private TbSellerMapper sellerMapper;


    @Override
    public TbSeller getSellerByName(String username) {
        TbSellerExample example = new TbSellerExample();
        example.createCriteria().andSellerIdEqualTo(username);
        List<TbSeller> tbSellers = sellerMapper.selectByExample(example);
        if (tbSellers != null && tbSellers.size() > 0) {
            return tbSellers.get(0);
        }
        return null;
    }

    @Override
    public TbSeller getSellerById(String sellerId) {
        return sellerMapper.selectByPrimaryKey(sellerId);
    }

    @Override
    public int register(TbSeller seller) {
        String salt = ShiroUtils.generateSalt(5);
//        System.out.println("盐" + salt);
        String md5 = ShiroUtils.encryptPassword("md5", seller.getPassword(), salt, 5);/*加密次数 和 解密次数成正比*/

        //判断name是否重复
        TbSellerExample example = new TbSellerExample();
        example.createCriteria().andSellerIdEqualTo(seller.getSellerId());
        List<TbSeller> tbSellers = sellerMapper.selectByExample(example);
        if (tbSellers.size() != 0) {
            return 999;//有数据说明有重复的sellerid
        }

        //不重复就增加密码和盐
        seller.setPassword(md5);
        seller.setSalt(salt);

        //增加 - 注册
        int i = sellerMapper.insertSelective(seller);
        return i;
    }

    @Override
    public int updateSeller(TbSeller seller) {
        int i = sellerMapper.updateByPrimaryKeySelective(seller);
        return i;
    }

    @Override
    public PageParams<SellerVO> getSellerList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSeller> tbSellers = sellerMapper.selectByExample(null);
        PageInfo<TbSeller> info=new PageInfo<>(tbSellers);
        List<SellerVO> collect = tbSellers.stream().map(seller -> {
            SellerVO sellerVO = new SellerVO();
            BeanUtils.copyProperties(seller, sellerVO);
            return sellerVO;
        }).collect(Collectors.toList());
        return new PageParams<>(info.getPageNum(),info.getPageSize(),collect,info.getTotal(),info.getPages());
    }

    @Override
    public List<SellerVO> getNameAndLinkeName(SellerVO sellerVO) {
        List<TbSeller> tbSellers = sellerMapper.getNameAndLinkeName(sellerVO);
        List<SellerVO> collect = tbSellers.stream().map(seller -> {
            SellerVO sellerVO1 = new SellerVO();
            BeanUtils.copyProperties(seller, sellerVO1);
            return sellerVO1;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SellerVO> getStatus(String status) {
        TbSellerExample example = new TbSellerExample();
        if (status != null){
            example.createCriteria().andStatusEqualTo(status);
        }
        List<TbSeller> tbSellers = sellerMapper.selectByExample(example);
        List<SellerVO> collect = tbSellers.stream().map(seller -> {
            SellerVO vo = new SellerVO();
            BeanUtils.copyProperties(seller, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public SellerVO getId(String sellerId) {
        TbSeller seller = sellerMapper.selectByPrimaryKey(sellerId);
        SellerVO sellerVO = new SellerVO();
        BeanUtils.copyProperties(seller,sellerVO);
        return sellerVO;
    }

    @Override
    public int SetAudit(String sellerId, Integer status) {
        int i = sellerMapper.SetAudit(sellerId,status);
        return i;
    }
}
